// pages/order/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderList: {},
        List: [{
            typeName: "全部",
            typeId: "4"
        }, {
            typeName: "未付款",
            typeId: "1"
        },
            {
                typeName: "待收货",
                typeId: "2"
            },
            {
                typeName: "已收货",
                typeId: "3"
            }
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const openId = wx.getStorageSync("openId")
        wx.request({
            url: 'https://mbp.jyccloud.cn/mbp/getOrderById',
            method: 'post',
            data: JSON.stringify({
                openId: openId
            }),
            success: (res) => {
                const list = res.data.orderList.map(item => {
                    console.log(res.data.productList.filter(data => item.productId.split(",").indexOf(data.productId.toString()) !== -1).map(p => {
                        return p.name
                    }))
                    return {
                        orderId: item.orderId,
                        imageUrl: res.data.productList.filter(data => item.productId.split(",").indexOf(data.productId.toString()) !== -1).map(p => {
                            return p.imageUrl
                        }),
                        productName: res.data.productList.filter(data => item.productId.split(",").indexOf(data.productId.toString()) !== -1).map(p => {
                            return p.name
                        }),
                        specName: res.data.specList.filter(data => item.specId.split(",").indexOf(data.specId.toString()) !== -1).map(p => {
                            return p.specName
                        }),
                        addressName: res.data.addressList.filter(data => data.addressId = item.addressId)[0].name,
                        orderState: item.orderState,
                        totalPrice: item.totalPrice,
                        address: res.data.addressList.filter(data => data.addressId = item.addressId)[0].address,
                        trackingNumber: item.trackingNumber,
                        count: item.count.split(",")
                    }
                })
                console.log(list)
                this.setData({
                    orderList: list
                })
            }
        })
    },

    delOrder(e) {
        console.log(e.target.id)
        const openId = wx.getStorageSync("openId")
        wx.request({
            url: 'https://mbp.jyccloud.cn/mbp/addOrderQuery',
            method: 'post',
            data: JSON.stringify({
                orderId: e.target.id,
                orderState: e.currentTarget.dataset["state"],
                totalPrice: e.currentTarget.dataset["price"],
                query: "删除",
                openId: openId
            }),

            success: (res) => {
                wx.showToast({
                    title: res.data,
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },

    editOrder(e) {
        wx.showToast({
            title: "抱歉，暂未开放此功能！",
            icon: 'none',
            duration: 2000
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})